using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Services;
using System;
using UserInterface.Misc;

namespace UserInterface
{
    public partial class ViewUser : BoardPageBase
    {
        private int userId;

        protected void Page_Load(object sender, EventArgs e)
        {
            userId = Convert.ToInt32(Request.QueryString["userId"]);
            User user = UserService.GetById(userId);

            string pageTitle = (string)GetLocalResourceObject("Title");
            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.TitleGenerator.ItemName = user.Username;
            MasterPage.HeaderText = pageTitle;

            MasterPage.AddPathItem((string)GetLocalResourceObject("Users"), "Users.aspx");
            MasterPage.AddPathItem(user.Username, "ViewUser.aspx?userId=" + userId);

            UsernameLiteral.Text = user.Username;
      
            if (user.AvatarUrl != "")
            {
                AvatarImage.ImageUrl = user.AvatarUrl;
            }
            else
            {
                AvatarImage.ImageUrl = "~/Templates/Default/Images/undefined.gif";
            }
            
            RealNameLiteral.Text = user.FirstName + " " + user.LastName;
            BirthdateLiteral.Text = OutputTools.FormatDate(user.Birthdate);
            if (user.Gender == Gender.Male)
            {
                GenderLiteral.Text = "Male";
            }
            else if (user.Gender == Gender.Female)
            {
                GenderLiteral.Text = "Female";
            }
            else
            {
                GenderLiteral.Text = "Not Specified";
            }

            WebsiteHyperLink.Text = user.WebsiteUrl;
            WebsiteHyperLink.NavigateUrl = user.WebsiteUrl;
            RegisteredLiteral.Text = OutputTools.FormatDate(user.RegisterDateTime);
            LastActiveLiteral.Text = OutputTools.FormatDate(user.LastActiveDateTime);
            TotalPostsLiteral.Text = user.Posts.ToString();
            PostsPerDayLiteral.Text = (user.Posts > 0 ? Math.Round((double)(user.Posts / DateTime.Now.Subtract(user.RegisterDateTime).Days), 2).ToString() : "0" );
        }
    }
}
